include (CMakeParseArguments)

# 产生android平台分发设置
function(GENERATED_DEPLOYMENT_SETTINGS)
    cmake_parse_arguments(PARA "" "NAME;APPLACTION" "" ${ARGN})

    if(NOT ANDROID_NDK)
        set(ANDROID_NDK $ENV{ANDROID_NDK})
        if(NOT ANDROID_NDK)
            set(ANDROID_NDK ${ANDROID_NDK_ROOT})
            if(NOT ANDROID_NDK)
                set(ANDROID_NDK $ENV{ANDROID_NDK_ROOT})
            endif()
        endif()
    endif()

    if(NOT ANDROID_SDK)
        set(ANDROID_SDK $ENV{ANDROID_SDK})
        if(NOT ANDROID_SDK)
            set(ANDROID_SDK ${ANDROID_SDK_ROOT})
            if(NOT ANDROID_SDK)
                set(ANDROID_SDK $ENV{ANDROID_SDK_ROOT})
            endif()
        endif()
    endif()

    if(NOT DEFINED BUILD_TOOS_VERSION)
        set(BUILD_TOOS_VERSION $ENV{BUILD_TOOS_VERSION})
    endif()
    if(NOT DEFINED BUILD_TOOS_VERSION)
        set(BUILD_TOOS_VERSION "28.0.3")
    endif()
    
    if(DEFINED PARA_NAME)
        set(_file_name ${PARA_NAME})
        #message("file_name:${PARA_NAME}")
    else()
        SET(_file_name "${PROJECT_BINARY_DIR}/android-lib${PROJECT_NAME}.so-deployment-settings.json")
    endif()

    FILE(WRITE ${_file_name} "{\n")
    FILE(APPEND ${_file_name} "\"description\": \"This file is generated by qmake to be read by androiddeployqt and should not be modified by hand.\",\n")
    FILE(APPEND ${_file_name} "\"qt\":\"${QT_INSTALL_DIR}\",\n")
    FILE(APPEND ${_file_name} "\"sdk\":\"${ANDROID_SDK}\",\n")
    FILE(APPEND ${_file_name} "\"sdkBuildToolsRevision\":\"${BUILD_TOOS_VERSION}\",\n")
    FILE(APPEND ${_file_name} "\"ndk\":\"${ANDROID_NDK}\",\n")

    FILE(APPEND ${_file_name} "\"stdcpp-path\":\"${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_shared.so\",\n")
    FILE(APPEND ${_file_name} "\"useLLVM\":true,\n")
    FILE(APPEND ${_file_name} "\"toolchain-prefix\":\"llvm\",\n")
    FILE(APPEND ${_file_name} "\"tool-prefix\":\"llvm\",\n")

    IF(CMAKE_HOST_WIN32)
        IF(ANDROID_NDK_HOST_X64)
            FILE(APPEND ${_file_name} "\"ndk-host\":\"windows-x86_64\",\n")
        ELSE()
            FILE(APPEND ${_file_name} "\"ndk-host\":\"windows\",\n")
        ENDIF()
    ELSE()
        IF(ANDROID_NDK_HOST_X64)
            FILE(APPEND ${_file_name} "\"ndk-host\":\"linux-x86_64\",\n")
	ELSE()
	    FILE(APPEND ${_file_name} "\"ndk-host\":\"linux\",\n")
        ENDIF()
    ENDIF()
    FILE(APPEND ${_file_name} "\"target-architecture\":\"${CMAKE_ANDROID_ARCH_ABI}\",\n")
    FILE(APPEND ${_file_name} "\"android-package-source-directory\":\"${PROJECT_SOURCE_DIR}/android\",\n")
    IF(ANDROID_EXTRA_LIBS)
        FILE(APPEND ${_file_name} "\"android-extra-libs\":\"${ANDROID_EXTRA_LIBS}\",\n")
    ENDIF(ANDROID_EXTRA_LIBS)
    if(DEFINED PARA_APPLACTION)
        FILE(APPEND ${_file_name} "\"application-binary\":\"${PARA_APPLACTION}\"\n")
        #message("app_bin:${PARA_APPLACTION}")
    else()
        FILE(APPEND ${_file_name} "\"application-binary\":\"${CMAKE_BINARY_DIR}/bin/lib${PROJECT_NAME}.so\"\n")
    endif()
    FILE(APPEND ${_file_name} "}")
endfunction(GENERATED_DEPLOYMENT_SETTINGS)

# 得到子目录
macro(SUBDIRLIST result curdir)
    file(GLOB children RELATIVE ${curdir} ${curdir}/*)
    set(dirlist "")
    foreach(child ${children})
        if(IS_DIRECTORY ${curdir}/${child})
            LIST(APPEND dirlist ${child})
        endif()
    endforeach()
    set(${result} ${dirlist})
endmacro()

# 安装目标文件
function(INSTALL_TARGETS_FILES)
    cmake_parse_arguments(PARA "" "" "TARGETS" ${ARGN})
    if(NOT DEFINED PARA_TARGETS)
        return()
    endif()

    foreach(component ${PARA_TARGETS})
        if(ANDROID)
            INSTALL(FILES $<TARGET_FILE:${component}>
                DESTINATION "libs/${ANDROID_ABI}"
                    COMPONENT Runtime)
        elseif(WIN32)
            INSTALL(FILES $<TARGET_FILE:${component}>
                DESTINATION "${CMAKE_INSTALL_BINDIR}"
                    COMPONENT Runtime)
        else()
            INSTALL(FILES $<TARGET_FILE:${component}>
                DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                    COMPONENT Runtime)
        endif()
    endforeach()
endfunction()

# 安装应用程序目标
function(INSTALL_TARGET)
    cmake_parse_arguments(PARA "ISEXE" "NAME;VERSION" "" ${ARGN})
    
    if(NOT DEFINED PARA_NAME)
        message(FATAL_ERROR "Use:
            INSTALL_TARGET(NAME name VERSION version)"
            )
    endif()
    
    if(NOT DEFINED PARA_VERSION)
        message(FATAL_ERROR "Use:
            INSTALL_TARGET(NAME name VERSION version)"
            )
    endif()
    
    if(${PARA_ISEXE})
        # Install target
        if(ANDROID)
            INSTALL(TARGETS ${PARA_NAME}
                RUNTIME DESTINATION "libs/${ANDROID_ABI}"
                    COMPONENT Runtime
                LIBRARY DESTINATION "libs/${ANDROID_ABI}"
                    COMPONENT Runtime
                ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                )
        elseif(WIN32)
            INSTALL(TARGETS ${PARA_NAME}
                RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
                    COMPONENT Runtime
                LIBRARY DESTINATION "${CMAKE_INSTALL_BINDIR}"
                    COMPONENT Runtime
                ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                )
        else()
            INSTALL(TARGETS ${PARA_NAME}
                RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
                    COMPONENT Runtime
                LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                    COMPONENT Runtime
                ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                )        
        endif()
    else()
        # 安装
        if(WIN32)
            INSTALL(TARGETS ${PARA_NAME}
                EXPORT ${PARA_NAME}Config
                RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
                COMPONENT Runtime
                LIBRARY DESTINATION "${CMAKE_INSTALL_BINDIR}"
                COMPONENT Runtime
                ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PARA_NAME}
                INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
                )
        else()
            INSTALL(TARGETS ${PARA_NAME}
                EXPORT ${PARA_NAME}Config
                RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
                COMPONENT Runtime
                LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                COMPONENT Runtime
                ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
                PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PARA_NAME}
                INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
                )
        endif()
        
        export(TARGETS ${PARA_NAME}
            APPEND FILE ${CMAKE_BINARY_DIR}/${PARA_NAME}Config.cmake
            )
        # Install cmake configure files
        install(EXPORT ${PARA_NAME}Config
            DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PARA_NAME}"
            )
        write_basic_package_version_file(
            "${CMAKE_BINARY_DIR}/${PARA_NAME}ConfigVersion.cmake"
            VERSION ${PARA_VERSION}
            COMPATIBILITY AnyNewerVersion)
        install(FILES "${CMAKE_BINARY_DIR}/${PARA_NAME}ConfigVersion.cmake"
            DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PARA_NAME}")  
    endif()
endfunction()
